<template>
    <div class="employee-container">
        <!-- 工具栏 -->
        <el-row :gutter="10" style="margin-bottom: 20px;">
            <el-col :span="4">
                <el-button type="primary" @click="openAddDialog">新增员工</el-button>
            </el-col>
        </el-row>

        <!-- 员工表格 -->
        <el-table :data="employees" border stripe style="width: 100%">
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="email" label="邮箱" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="position" label="职位" />
            <el-table-column prop="deptName" label="所属部门" />
            <el-table-column prop="createdAt" label="创建时间" width="160" />
            <el-table-column prop="updatedAt" label="更新时间" width="160" />

            <el-table-column label="操作" width="180">
                <template #default="{ row }">
                    <el-button size="small" @click="openEditDialog(row)">编辑</el-button>
                    <el-button size="small" type="danger" @click="deleteEmployee(row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[5, 10, 20]"
            layout="total, sizes, prev, pager, next" :total="total" @size-change="loadEmployees"
            @current-change="loadEmployees" />

        <!-- 弹窗：新增 / 编辑 -->
        <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑员工' : '新增员工'">
            <el-form ref="formRef" :model="form" label-width="100px" :rules="rules" @submit.prevent="submitForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" />
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="form.phone" />
                </el-form-item>
                <el-form-item label="职位" prop="position">
                    <el-input v-model="form.position" />
                </el-form-item>
                <el-form-item label="头像地址" prop="avatarUrl">
                    <el-input v-model="form.avatarUrl" />
                </el-form-item>
                <el-form-item label="所属部门" prop="deptId">
                    <el-select v-model="form.deptId" placeholder="请选择部门" style="width: 100%">
                        <el-option v-for="dept in allDepts" :key="dept.id" :label="dept.name" :value="dept.id" />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitForm">确认</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import request from '@/utils/request';
import { ElMessage } from 'element-plus';

const employees = ref([]);
const depts = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const allDepts = ref([]);
const dialogVisible = ref(false);
const isEdit = ref(false);
const form = ref({
    id: null,
    name: '',
    email: '',
    phone: '',
    position: '',
    avatarUrl: '',
    deptId: null,
});
const rules = {}; // 可选校验规则

// 加载员工列表
const loadEmployees = () => {
    request.get('/employee/list', {
        params: {
            pageNum: pageNum.value,
            pageSize: pageSize.value
        }
    }).then(res => {
        employees.value = res.data;
        total.value = res.total;
    });
};

// 加载所有部门用于下拉框
const loadDepts = () => {
    request.get('/dept/list', {
        params: {
            pageNum: pageNum.value,
            pageSize: pageSize.value
        }
    }).then(res => {
        allDepts.value = res.data.records
        console.log(res.data.records);
    });
};

// 打开新增窗口
const openAddDialog = () => {
    isEdit.value = false;
    form.value = {
        id: null,
        name: '',
        email: '',
        phone: '',
        position: '',
        avatarUrl: '',
        deptId: null,
    };
    dialogVisible.value = true;
};

// 打开编辑窗口
const openEditDialog = (row) => {
    isEdit.value = true;
    form.value = { ...row };
    dialogVisible.value = true;
};

// 提交表单
const submitForm = () => {
    const url = isEdit.value ? '/employee' : '/employee';
    const method = isEdit.value ? 'put' : 'post';
    console.log(form.value);
    request({
        url,
        method,
        data: form.value
    }).then(() => {
        ElMessage.success('操作成功');
        dialogVisible.value = false;
        loadEmployees(); // 刷新表格
    });
};

// 删除员工
const deleteEmployee = (id) => {
    request.delete(`/employee/${id}`).then(() => {
        ElMessage.success('删除成功');
        loadEmployees();
    });
};

onMounted(() => {
    loadEmployees();
    loadDepts();
});
</script>

<style lang="scss" scoped>
.employee-container {
    padding: 20px;
}
</style>